package com.yunhe.merchants.controller;

import com.alibaba.excel.EasyExcel;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.listener.UploadDataListener;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.utils.ExcelUtils;
import com.yunhe.merchants.domain.MyProject;
import com.yunhe.merchants.domain.ProcessStatus;
import com.yunhe.merchants.model.FilterCondition;
import com.yunhe.merchants.model.HousingMerchantsInfo;
import com.yunhe.merchants.model.IntentionProjectUpload;
import com.yunhe.merchants.model.SuccessProjectUpload;
import com.yunhe.merchants.service.IDataHistoryProjectStatusService;
import com.yunhe.merchants.service.ISuccessProjectService;
import com.yunhe.merchants.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: DongWenYu
 * @Date: 2021/5/7 22:17
 */
@Api(value = "talk", description = "成功项目", tags = {"Model_Success_Project"})
@RestController
@RequestMapping("/success")
public class SuccessProjectController {

    @Autowired
    MyHeaderUtil headerUtil;

    @Autowired
    ISuccessProjectService successProjectService;

    @Autowired
    IDataHistoryProjectStatusService dataHistoryProjectStatusService;

    @ApiOperation("成功项目条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping
    public ResponseEntity<ResultObject> selectByCondition(@ModelAttribute FilterCondition filterCondition,
                                                              @RequestParam("page") int page,
                                                              @RequestParam("size") int size) throws ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        PageResult result = successProjectService.selectByFilterCondition(filterCondition,userId,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("导入成功项目")
    @PostMapping("/upload")
    public ResponseEntity<ResultObject> upload(@RequestBody MultipartFile file,
                                               @RequestParam("stationId") Long stationId) throws IOException, ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        EasyExcel.read(file.getInputStream(), SuccessProjectUpload.class,
                new UploadDataListener(successProjectService,file.getOriginalFilename(),userId,stationId)).sheet().doRead();
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("导出成功项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "成功项目的id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/export")
    public ResponseEntity<ResultObject> export(@RequestParam("ids") String ids,
                                               @ModelAttribute FilterCondition filterCondition,
                                               HttpServletResponse httpServletResponse) {
        List<MyProject> data = successProjectService.export(ids,filterCondition);
        ExcelUtils.convertAndExport(data , httpServletResponse , SuccessProjectUpload.class);
        return null;
    }

    @ApiOperation("查看成功项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "成功项目id", dataType = "long", paramType = "query")
    })
    @GetMapping("/detail")
    public ResponseEntity<ResultObject> selectById(@RequestParam("id") Long id){
        MyProject result = successProjectService.selectById(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("查看成功项目的历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "成功id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping("/history")
    public ResponseEntity<ResultObject> selectHistory(@RequestParam("id") Long id,
                                                      @RequestParam("page") int page,
                                                      @RequestParam("size") int size){
        PageResult result = dataHistoryProjectStatusService.selectByProjectId(id,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("查看成功项目状态流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query")
    })
    @GetMapping("/status")
    public ResponseEntity<ResultObject> selectStatus(@RequestParam("id") Long id,
                                                     @RequestParam(value = "status", required = false) String status){
        ProcessStatus result = successProjectService.selectStatusById(id ,status);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("房源管理查看招商信息")
    @ApiImplicitParam(name = "id", value = "公司id", dataType = "long", paramType = "query")
    @GetMapping("/housingMerchantsInfo")
    public ResponseEntity<ResultObject<HousingMerchantsInfo>> getMerchantsInfo(@RequestParam("id") Long id){
        HousingMerchantsInfo housingMerchantsInfo = successProjectService.getMerchantsInfo(id);
        return new ResponseEntity<>(new ResultObject<>(housingMerchantsInfo),HttpStatus.OK);
    }
}
